package cn.itcast.hotel;

import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.Operator;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.IOException;

/**
 * TODO
 *
 * @Author miss.fu
 * @date 2025/1/8 21:31
 */
@SpringBootTest
public class test1 {

    @Autowired
    private RestHighLevelClient client;
    @Test
    public void queryStringQuery() throws IOException {
        // 1.创建请求对象
        SearchRequest request = new SearchRequest("hotel");
        // 2.构建结果类型 和 条件类型
        request.source().query(QueryBuilders.queryStringQuery("万怡").field("brand").field("city").defaultOperator(Operator.AND));
        // 3.执行查询
        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
        // 4.解析响应结果
        SearchHits hits = response.getHits();
        long total = hits.getTotalHits().value;
        System.out.println("总数:"+total);
        for (SearchHit hit : hits.getHits()) {
            System.out.println("酒店数据:"+hit.getSourceAsString());
        }
    }
}
